package com.sy.msgview

import android.content.UriMatcher
import android.content.res.Configuration
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.SystemBarStyle
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.activity.result.contract.ActivityResultContracts
import androidx.activity.viewModels
import androidx.compose.animation.AnimatedVisibility
import androidx.compose.animation.core.MutableTransitionState
import androidx.compose.animation.fadeOut
import androidx.compose.animation.shrinkVertically
import androidx.compose.animation.slideInVertically
import androidx.compose.animation.slideOutVertically
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.displayCutoutPadding
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.imePadding
import androidx.compose.foundation.layout.navigationBarsPadding
import androidx.compose.foundation.layout.systemBarsPadding
import androidx.compose.material3.Divider
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import android.graphics.Color
import android.net.Uri
import android.widget.Toast
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.core.view.WindowCompat
import androidx.navigation.NavController
import androidx.navigation.NavType
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import androidx.navigation.navArgument
import com.sy.msgview.component.Hitokoto
import com.sy.msgview.component.MsgView
import com.sy.msgview.component.QQListView
import com.sy.msgview.ui.theme.MsgViewTheme
import java.io.FileOutputStream
import java.io.IOException
import java.io.InputStream

class MainActivity : ComponentActivity() {
	private val model: QQViewModel by viewModels()

	override fun onCreate(savedInstanceState: Bundle?) {
		super.onCreate(savedInstanceState)
//		WindowCompat.setDecorFitsSystemWindows(window,false)
//		rememberSys
//		enableEdgeToEdge(SystemBarStyle.auto(Color.RED, Color.RED))
		val intent = intent
		if (intent.action == "android.intent.action.VIEW") {
			Utils.showLog(intent.data)
			intent.data?.let {
				try {
					model.uploadDatabase(it)
					Toast.makeText(this, "上传成功", Toast.LENGTH_SHORT).show()
				} catch (e: IOException) {
					e.printStackTrace()
					Toast.makeText(this, "上传失败", Toast.LENGTH_SHORT).show()
				}
			}
		}

		setContent {
			MsgViewTheme {
				val navController = rememberNavController()
				NavHost(navController = navController, startDestination = Screen.MAIN.route) {
					composable("main") {
						NavEnterAndExitAnimation {
							MainView(model, navController)
						}
					}
					composable("msg/{qq}", listOf(navArgument("qq") { NavType.LongType })) {
						NavEnterAndExitAnimation {
							it.arguments?.let {
								val qq = it.getString("qq")?.toLong()
								qq?.let { it1 -> MsgView(model, it1, navController) }
							}
						}
					}
				}
			}
		}
	}

	private fun fileSelector() {

		val selector = registerForActivityResult(ActivityResultContracts.GetContent()) {
			it?.let { Utils.showLog(it.toString()) }
		}
//		selector.launch()
	}


}


@Composable
fun MainView(model: QQViewModel, navController: NavController) {
	Surface(
		modifier = Modifier.fillMaxSize(),
		color = MaterialTheme.colorScheme.background
	) {
		Column(
			modifier = Modifier
//				.systemBarsPadding()
//				.navigationBarsPadding()
//				.imePadding()
//				.displayCutoutPadding()
		) {
			Hitokoto()
			Divider(
				modifier = Modifier,
				thickness = 1.dp,
				color = androidx.compose.ui.graphics.Color.Gray
			)
			QQListView(model, navController)
		}
	}
}

@Composable
fun NavEnterAndExitAnimation(content: @Composable () -> Unit) {
	val visibleState = remember { MutableTransitionState(initialState = false) }
	AnimatedVisibility(
		visibleState = visibleState.apply {
			targetState = true
		},
		enter = slideInVertically(
			initialOffsetY = { -200 }
		),
		exit = slideOutVertically() + shrinkVertically() + fadeOut(),
	) {
		content()
	}
}

@Preview(
	apiLevel = 33,
	showSystemUi = true, uiMode = Configuration.UI_MODE_NIGHT_YES or Configuration.UI_MODE_TYPE_NORMAL,
)
@Composable
fun GreetingPreview() {
	MsgViewTheme {
//		MainView()
	}
}
